<template>
    <div class="wrapper">
        <img class="wrapper__img" src="https://www.dell-lee.com/imgs/vue3/user.png"/>
        <div class="wrapper__input">
            <input class="wrapper__input__content" placeholder="请输入用户名" v-model="username" />
        </div>
        <div class="wrapper__input">
            <input class="wrapper__input__content" placeholder="请输入密码" type="password" v-model="password" autocomplete="new-password" />
        </div>
        <div class="wrapper__login-button" @click="handleLogin">登陆</div>
        <div class="wrapper__login-link">
            <span @click="handleRegister">立即注册</span>
            <span class="wrapper__login-link__split">|</span>
            <span @click="handleForgetPassword">忘记密码</span>
        </div>
        <Toast v-if="show" :message="toastMessage" />
    </div>
</template>

<script>
import { reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
import { post } from '../../utils/request'
import Toast, { userToastEffect } from '../../components/Toast'

// 用户登陆逻辑
const userLoginEffect = (showToast) => {
  const router = useRouter()
  const data = reactive({ username: '', password: '' })
  const handleLogin = async () => {
    try {
      const { username, password } = data
      if (username === '' || username.length === 0) {
        showToast('请输入用户名')
        return
      }
      if (password === '' || password.length === 0) {
        showToast('请输入密码')
        return
      }
      const result = await post('/api/user/login', {
        username: data.username,
        password: data.password
      })
      if (result?.errno === 0) {
        localStorage.isLogin = true
        router.push({ name: 'Home' })
      } else {
        showToast('登录失败')
      }
    } catch (error) {
      showToast('请求失败')
    }
  }
  const { username, password } = toRefs(data)
  return { username, password, handleLogin }
}

// 用户注册路由跳转
const userRegisterRouterEffect = () => {
  const router = useRouter()
  const handleRegister = () => {
    router.push({ name: 'Register' })
  }
  return { handleRegister }
}

// 用户忘记密码路由跳转
const userForgetPasswordRouterEffect = () => {
  const router = useRouter()
  const handleForgetPassword = () => {
    router.push({ name: 'ForgetPassword' })
  }
  return { handleForgetPassword }
}

export default {
  name: 'Login',
  components: {
    Toast
  },
  // setup的职责就是告诉你代码执行的流程
  setup () {
    const { show, toastMessage, showToast } = userToastEffect()
    const { username, password, handleLogin } = userLoginEffect(showToast)
    const { handleRegister } = userRegisterRouterEffect()
    const { handleForgetPassword } = userForgetPasswordRouterEffect()

    return {
      username,
      password,
      show,
      toastMessage,
      handleLogin,
      handleRegister,
      handleForgetPassword
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../style/variables.scss";
.wrapper {
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    transform: translateY(-50%);
    &__img {
        display: block;
        margin: 0 auto .4rem auto;
        width: .66rem;
        height: .66rem;
    }
    &__input {
        height: .48rem;
        margin: 0 .4rem .16rem .4rem;
        padding: 0 .16rem;
        background: #F9F9F9;
        border: .01rem solid rgba(0,0,0,0.10);
        border-radius: .06rem;
        &__content {
            line-height: .48rem;
            width: 100%;
            border: none;
            outline: none;
            background: none;
            font-size: .16rem;
            color: $content-notice-font-color;
            &::placeholder {
                color: $content-notice-font-color;
            }
        }
    }
    &__login-button {
        margin: .32rem .4rem .16rem .4rem;
        line-height: .48rem;
        font-size: .16rem;
        background: #0091FF;
        color: #FFFFFF;
        text-align: center;
        box-shadow: 0 .04rem .08rem 0 rgba(0,145,255,0.32);
        border-radius: .04rem;
    }
    &__login-link {
        text-align: center;
        margin-top: .16rem;
        font-size: .14rem;
        color: rgba(0,0,0,0.50);
        &__split {
            margin: .02rem .12rem 0 .12rem;
            font-size: .12rem;
            line-height: .12rem;
            color: $content-notice-font-color;
        }
    }
}
</style>
